package com.ydd.adapter;

import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.taofengsdk.adutil.Utils;
import com.taofengsdk.video.VideoAdManager;
import com.taofengsdk.widegt.CustomVideoView;
import com.ydd.R;
import com.ydd.model.recommand.RecommandBodyValue;
import com.ydd.util.Util;
import com.ydd.util.glide.GlideManager;

import java.util.List;

/**
 * @author: tao
 * @function:
 * @date: 17/3/14
 */
public class HomeFragmentAdapter extends BaseMultiItemQuickAdapter<RecommandBodyValue> {
    /**
     * Common
     */
    private static final int VIDOE_TYPE = 0;
    private static final int CARD_TYPE_ONE = 1;
    private static final int CARD_TYPE_TWO = 2;
    private static final int CARD_TYPE_THREE = 3;

    private VideoAdManager mVideoManager;


    public HomeFragmentAdapter(List<RecommandBodyValue> data) {
        super(data);
        addItemType(VIDOE_TYPE, R.layout.item_video_layout);
        addItemType(CARD_TYPE_ONE, R.layout.item_product_card_one_layout);
        addItemType(CARD_TYPE_TWO, R.layout.item_product_card_two_layout);
        addItemType(CARD_TYPE_THREE, R.layout.item_product_card_three_layout);

    }

    public void updateVideoInScrollView() {
        if (mVideoManager != null) {
            mVideoManager.updateVideoInScrollView();
        }
    }

    public void changeFagmentOrActivity() {
        if (mVideoManager != null) {
            mVideoManager.changeFagmentOrActivity();
        }
    }


    @Override
    protected void convert(final BaseViewHolder holder, final RecommandBodyValue value) {
        switch (holder.getItemViewType()) {
            case VIDOE_TYPE:
                RelativeLayout mVideoContentLayout = holder.getView(R.id.video_ad_layout);
                mVideoManager = new VideoAdManager(mVideoContentLayout, new Gson().toJson(value),
                        new CustomVideoView.ADFrameImageLoadListener() {
                            @Override
                            public void onStartFrameLoad(String url,
                                                         CustomVideoView.ImageLoaderListener listener) {

                            }
                        });

                break;
            case CARD_TYPE_ONE:
                GlideManager.loadImageView(mContext, value.getLogo(), (ImageView) holder
                        .getView(R.id.item_logo_view));
//                holder.setText(R.id.title_view, value.title);
                holder.setText(R.id.item_price_view, value.getPrice());
                holder.setText(R.id.item_info_view, value.getInfo().concat(mContext.getString(R.string.tian_qian)));
                holder.setText(R.id.item_footer_view, value.getText());
                holder.setText(R.id.item_from_view, value.getFrom());
                holder.setText(R.id.item_zan_view, mContext.getString(R.string.dian_zan).concat(value.getZan()));

                LinearLayout mProductLayout = holder.getView(R.id.product_photo_layout);
                for (String url : value.getUrl()) {
                    mProductLayout.addView(createImageView(url));
                }
                holder.setOnClickListener(R.id.product_photo_layout, new OnItemChildClickListener());
                break;
            case CARD_TYPE_TWO:
                GlideManager.loadImageView(mContext, value.getLogo(), (ImageView) holder
                        .getView(R.id.item_logo_view));
//                holder.setText(R.id.title_view, value.title);
                holder.setText(R.id.item_price_view, value.getPrice());
                holder.setText(R.id.item_info_view, value.getInfo().concat(mContext.getString(R.string.tian_qian)));
                holder.setText(R.id.item_footer_view, value.getText());
                holder.setText(R.id.item_from_view, value.getFrom());
                holder.setText(R.id.item_zan_view, mContext.getString(R.string.dian_zan).concat(value.getZan()));
                GlideManager.loadImageView(mContext, value.getUrl().get(0),
                        (ImageView) holder.getView(R.id.product_photo_view));

                holder.setOnClickListener(R.id.product_photo_view, new OnItemChildClickListener());
                break;
            case CARD_TYPE_THREE:
                ViewPager mViewPager = (ViewPager) holder.getView(R.id.pager);
                //add data
                List<RecommandBodyValue> recommandList = Util.handleData(value);
                mViewPager.setPageMargin(Utils.dip2px(mContext, 12));
                mViewPager.setAdapter(new HotSalePagerAdapter(mContext, recommandList));
                mViewPager.setCurrentItem(recommandList.size() * 100);

                break;
        }
    }


    //动态添加ImageView
    private ImageView createImageView(String url) {
        ImageView imageView = new ImageView(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 100),
                ViewGroup.LayoutParams.MATCH_PARENT);
        params.leftMargin = Utils.dip2px(mContext, 5);
        imageView.setLayoutParams(params);
        GlideManager.loadImageView(mContext, url, imageView);
        return imageView;
    }
}
